package com.example.administrator.yanfoxconn.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.administrator.yanfoxconn.R;
import com.example.administrator.yanfoxconn.adapter.LMListAdapter;
import com.example.administrator.yanfoxconn.bean.LMIE8S;
import com.example.administrator.yanfoxconn.constant.Constants;
import com.example.administrator.yanfoxconn.constant.FoxContext;
import com.example.administrator.yanfoxconn.utils.BaseActivity;
import com.example.administrator.yanfoxconn.utils.HttpConnectionUtil;
import com.example.administrator.yanfoxconn.utils.HttpUtils;
import com.example.administrator.yanfoxconn.utils.ToastUtils;
import com.example.administrator.yanfoxconn.widget.MyListView;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @Description IE8S 季度稽核和專項稽核 項目介面
 * @Author song
 * @Date 2/19/22 9:22 AM
 */
public class LMMenuListActivity extends BaseActivity  implements View.OnClickListener{
    private final int MESSAGE_SET_JC=111;
    private final int MESSAGE_SET_YEAR=222;

    @BindView(R.id.btn_title_left)
    Button btnBack;//返回
    @BindView(R.id.btn_title_right)
    Button btnAdd;//新增
    @BindView(R.id.tv_title)
    TextView tvTitle;//標題
    @BindView(R.id.sp_jd_year)
    Spinner spJdYear;//季度稽核，選擇年份
    @BindView(R.id.mlv)
    MyListView mlv;

    private String from,flag1;//來源類別
    private List<LMIE8S> msgList;
    private LMListAdapter lmListAdapter;
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lm_menu_list);
        ButterKnife.bind(this);

        btnBack.setOnClickListener(this);
        btnAdd.setOnClickListener(this);
        btnAdd.setVisibility(View.VISIBLE);
        btnAdd.setText("新增");
        tvTitle.setText(getIntent().getStringExtra("title"));
        from = FoxContext.getInstance().getType();
        flag1 = getIntent().getStringExtra("flag1");


            mlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(LMMenuListActivity.this,LMListActivity.class);
                    intent.putExtra("title", msgList.get(position).getType());
                    if (flag1.equals("jdjh")){
                        intent.putExtra("zx_year", year);
                    }
                    intent.putExtra("flag1", flag1);
                    startActivity(intent);
                }
            });
        if (flag1.equals("jdjh")) {
            getYear();

        }else {
           //getList(FoxContext.getInstance().getFlag(), FoxContext.getInstance().getProduce_place(), FoxContext.getInstance().getDepart(), flag1,"");

        }


    }


    /*IE8S 獲取數據*/
    public void getList(String flag,String product,String depart,String flag1,String jdYear) {

        showDialog();
        final String url = Constants.HTTP_LM_GET_JD_NUMBERS  ;

        JsonObject object = new JsonObject();
        object.addProperty("flag", flag);
        object.addProperty("product", product);
        object.addProperty("depart", depart);
        object.addProperty("flag1", flag1);
        object.addProperty("zx_year", jdYear);


        //開啟一個新執行緒，向伺服器上傳資料
        new Thread() {
            public void run() {
                //把网络访问的代码放在这里
                try {
                    Log.e("---------", "==fff===" + url);
                    String result = HttpConnectionUtil.doPostJsonObject(url, object);
                    Gson gson = new Gson();
                    Log.e("---------", "object==fff===" + object.toString());
                    if (result != null) {
                        dismissDialog();
                        Log.e("---------", "result==fff===" + result);
                        JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                        String errCode = jsonObject.get("code").getAsString();
                        if (errCode.equals("200")) {
                            JsonObject jsonObjectData = jsonObject.get("data").getAsJsonObject();
                            JsonArray array = jsonObjectData.get("getListJd1").getAsJsonArray();
                            msgList = new ArrayList<LMIE8S>();
                            for (JsonElement type : array) {
                                LMIE8S humi = gson.fromJson(type, LMIE8S.class);
                                msgList.add(humi);
                            }
                            Message message = new Message();
                            message.what = MESSAGE_SET_JC;
                            message.obj = jsonObject.get("message").getAsString();
                            mHandler.sendMessage(message);
                        } else{
                            Message message = new Message();
                            message.what = Constants.MESSAGE_TOAST;
                            message.obj = jsonObject.get("message").getAsString();
                            mHandler.sendMessage(message);
                        }
                    }else{
                        Message message = new Message();
                        message.what = Constants.MESSAGE_NETMISTAKE;
                        mHandler.sendMessage(message);
                        finish();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
    String getYearList,year="";
    //基本信息下拉
    private void getYear(){
        showDialog();
        final String url = Constants.HTTP_LM_GET_JD_YEAR  ;

        JsonObject object = new JsonObject();

        object.addProperty("user_id", FoxContext.getInstance().getLoginId());
        //開啟一個新執行緒，向伺服器上傳資料
        new Thread() {
            public void run() {
                //把网络访问的代码放在这里

                Log.e("---------", "==fff===" + url);
                String result = null;
                try {
                    result = HttpConnectionUtil.doPostJsonObject(url, object);

                Gson gson = new Gson();
                Log.e("---------", "object==fff===" + object.toString());
                if (result != null) {
                    dismissDialog();
                    Log.e("---------", "result==fff===" + result);
                    JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                    String errCode = jsonObject.get("code").getAsString();
                    if (errCode.equals("200")) {
                        dismissDialog();
                        Log.e("----result-----", result);

                        int i = result.split("\\:").length;
                        getYearList = result.split("\\:")[i - 1];
                        Message message = new Message();
                        message.what = MESSAGE_SET_YEAR;
                        mHandler.sendMessage(message);
                    } else {
                        Message message = new Message();
                        message.what = Constants.MESSAGE_NETMISTAKE;
                        message.obj = "網絡異常！";
                        mHandler.sendMessage(message);
                    }
                }
            } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private void setYear(){
        String[] zxInfo = getYearList.replaceAll("\"","").replaceAll("\\}","").replaceAll("\\[","").replaceAll("\\]","").split(",");

        List<String> zxName=new ArrayList<>();
        zxName.add("請選擇年份");
        for (int i = 0;i<zxInfo.length;i++){
            zxName.add(zxInfo[i]);
        }
        spJdYear.setVisibility(View.VISIBLE);
        //下拉列表選擇
        spJdYear.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, zxName));
        spJdYear.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                year=zxName.get(position);
                if(year.equals("請選擇年份")){
                    
                }else {
                    getList(FoxContext.getInstance().getFlag(), FoxContext.getInstance().getProduce_place(), FoxContext.getInstance().getDepart(), flag1, year);
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }
    Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_SET_JC:
                    lmListAdapter = new LMListAdapter(LMMenuListActivity.this,msgList,flag1);
                    mlv.setAdapter(lmListAdapter);
                    break;
                    case MESSAGE_SET_YEAR:
                    setYear();
                    break;
                case Constants.MESSAGE_TOAST://Toast
                    ToastUtils.showShort(LMMenuListActivity.this, msg.obj.toString());
                    break;
                case Constants.MESSAGE_NETMISTAKE://Toast
                    ToastUtils.showLong(LMMenuListActivity.this,R.string.net_mistake);
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_title_left:
                finish();
                break;
            case R.id.btn_title_right:
                Intent intent = new Intent(LMMenuListActivity.this,LMExceDetailActivity.class);
                intent.putExtra("from","add");
                intent.putExtra("flag1",flag1);
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (flag1.equals("jdjh")) {
//            getYear();

        }else {
            getList(FoxContext.getInstance().getFlag(), FoxContext.getInstance().getProduce_place(), FoxContext.getInstance().getDepart(), flag1,"");

        }
    }
}
